<?php
    session_start();
 /*  if(!$_SESSION['LOGGED_IN'])
   {
       header( 'Location: index.html' ) ;
   }*/
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <LINK REL=StyleSheet HREF="maintenancePagesCSS.css" TYPE="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body onload="document.getElementById('txtAreaNotes').focus();">
        <?php
            include "EmployeeDao.php";
            include "Datasource.php";
            include "Employee.php";
            $objEmployeeDAO = new EmployeeDao();
            $objDatasource = new Datasource("localhost", "theanykey", "root", "");
            $objEmployee = $objEmployeeDAO->getObject($objDatasource, $_SESSION['userId']);
            $empName = $objEmployee->getLastName() . ", " . $objEmployee->getFirstName();

         ?>
        <center>
            <table class="EditTable">
                <tr>
                    <th>
                        Notes:&nbsp;
                    </th>
                    <td>
                        <textarea id="txtAreaNotes" rows="4" cols="40"></textarea>
                        <script type="text/javascript">
                        </script>
                    </td>
                </tr>
                <tr>
                    <th>
                        # Hours:&nbsp;
                    </th>
                    <td>
                        <input type="text" id="txtHours" />
                    </td>
                </tr>
                <tr>
                    <th>
                        Date/Time Stamp:&nbsp;
                    </th>
                    <td>
                        <input type="text" id="txtDateDoneMonth" style="width:24px" maxlength="2" />&nbsp;<b>/</b>
                        <input type="text" id="txtDateDoneDay" style="width:24px" maxlength="2"/>&nbsp;<b>/</b>
                        <input type="text" id="txtDateDoneYear" style="width:40px" maxlength="4"/>&nbsp;&nbsp;<b>Time:</b>&nbsp;&nbsp;
                        <input type="text" id="txtDateDoneHour" style="width:24px" maxlength="2"/>&nbsp;<b>:</b>
                        <input type="text" id="txtDateDoneMinute" style="width:24px" maxlength="2"/>&nbsp;&nbsp;
                        <select id="selBox">
                            <option value="AM">
                                AM
                            </option>
                            <option value="PM">
                                PM
                            </option>
                        </select>&nbsp;&nbsp;
                        <input type="button" value="Now" onclick="fillTime();"
                    </td>
                </tr>
            </table>
                <br/>
                <input type="button" value="Submit" id="btnSubmitWorkDone" onclick="submitForm();" />
        </center>
    </body>
    <script type="text/javascript">
        strBackgroundColor = "Yellow";
        function fillTime()
        {
            var month = document.getElementById("txtDateDoneMonth");
            var day = document.getElementById("txtDateDoneDay");
            var year = document.getElementById("txtDateDoneYear");
            var hour = document.getElementById("txtDateDoneHour");
            var minute = document.getElementById("txtDateDoneMinute");
            var date = new Date();
            month.value = date.getMonth() + 1;
            day.value = date.getDate();
            year.value = date.getFullYear();
            var tempHour = date.getHours();
            var ampm = document.getElementById("selBox");
            if(tempHour > 12)
            {
                ampm.selectedIndex = 1;
            }
            else
            {
                ampm.selectedIndex = 0;
            }
            hour.value = tempHour;
            minute.value =  date.getMinutes();
            if(minute.value.length < 2)
            {
                minute.value = "0" + date.getMinutes();
            }
            
        }

function submitForm()
{
    month = document.getElementById("txtDateDoneMonth");
    day = document.getElementById("txtDateDoneDay");
    year = document.getElementById("txtDateDoneYear");
    hour = document.getElementById("txtDateDoneHour");
    minute = document.getElementById("txtDateDoneMinute");
    AMPM = document.getElementById("selBox");

    strFix = "Please fix the following to submit:";
    strCompare = strFix;
    if(document.getElementById("txtAreaNotes").value == "")
    {
        strFix += "\n  - Please enter some Notes";
    }
    if(isNaN(document.getElementById("txtHours").value) || document.getElementById("txtHours").value == "")
    {
        strFix += "\n  - Number of hours needs to be a number";
    }
    if(!isValidDate())
    {
        strFix += "\n  - Please enter a valid date: MM/DD/YYYY HH:MM AM/PM";
    }
    if(strFix != strCompare)
    {
        alert(strFix);
    }
    else
    {
      //passes and will be written to the page
      var wdNotes = window.opener.document.getElementById("wdNotes");
      var wdDate = window.opener.document.getElementById("wdDateDone");
      var wdHours = window.opener.document.getElementById("wdHours");

      wdNotes.value = document.getElementById("txtAreaNotes").value;
      wdHours.value = document.getElementById("txtHours").value;

      strDate = month.value + "/" + day.value + "/" + year.value + " "
              + hour.value + ":" + minute.value + " " + AMPM[AMPM.selectedIndex].value;
      strDateTemp = strDate.substr(0, 10);
      strHoursMinsTemp = strDate.substr(10, strDate.length - 12).trim() + ":00";
      strArrayTemp = new Array();
      strArrayTemp = strDateTemp.split("/");
      strDateTemp = strArrayTemp[2].trim() + "-" + strArrayTemp[0].trim()  + "-" + strArrayTemp[1].trim()
                + " " + strHoursMinsTemp;

      wdDate.value = strDateTemp;
     //this was used with the old logic. Just going to add the checked
     //values to the textboxes
     
     /* strNotes = document.getElementById("txtAreaNotes").value;
      numHours = document.getElementById("txtHours").value;
      strDate = month.value + "/" + day.value + "/" + year.value + " "
              + hour.value + ":" + minute.value + " " + AMPM[AMPM.selectedIndex].value;

      otherWindow = window.opener;
      writeTbl =  otherWindow.document.getElementById("tblWorkDone");
      if(!writeTbl.rows)
      {
        //IE Bug, IE8 Blows!!!
          //rows = writeTbl.getElementsByTagName("TR");
          //iteration = rows.length;
          alert("This functionality currently does not work in Internet Explorer. Please Download Firefox from :\n\n     www.mozilla.com\n\nand try again");
         window.close();
         return;
      }

      iteration = writeTbl.rows.length;
      var row = writeTbl.insertRow(writeTbl.rows.length);

      //user assigned
      td = row.insertCell(0);
      td.style.textAlign = "center";
      hiddenInName = otherWindow.document.createElement("input");
      hiddenInName.type = "hidden";
      hiddenInName.value = "<?php echo $_SESSION['userId'];?>";
      hiddenInName.name = "wdUser" + iteration;
      hiddenInName.id = "wdUser" + iteration;
      //use the get user function of the employee class here
    
      td.innerHTML =  "<?php echo $empName; ?>";
      td.appendChild(hiddenInName);

      //date
      td2 = row.insertCell(1);
      td2.style.textAlign = "center";
      hiddenInDate = otherWindow.document.createElement("input");
      hiddenInDate.type = 'hidden';
      strDateTemp = strDate.substr(0, 10);
      strHoursMinsTemp = strDate.substr(10, strDate.length - 12).trim() + ":00";
      strArrayTemp = new Array();
      strArrayTemp = strDateTemp.split("/");
      strDateTemp = strArrayTemp[2].trim() + "-" + strArrayTemp[0].trim()  + "-" + strArrayTemp[1].trim()
                + " " + strHoursMinsTemp;
      hiddenInDate.value = strDateTemp;
      hiddenInDate.name = "wdDate" + iteration;
      hiddenInDate.id = "wdDate" + iteration;
      td2.innerHTML = strDateTemp;
      td2.appendChild(hiddenInDate);

      //Notes
      td3 = row.insertCell(2);
      td3.style.textAlign = "center";
      hiddenInNotes =  otherWindow.document.createElement("input");
      hiddenInNotes.type = 'hidden';
      hiddenInNotes.value = strNotes;
      hiddenInNotes.name = "wdNotes" + iteration;
      hiddenInNotes.id = "wdNotes" + iteration;
      td3.innerHTML = strNotes.length > 30 ? (strNotes.substr(0, 30) + "...") : strNotes;
      td3.title = strNotes;
      td3.appendChild(hiddenInNotes);

      //Number of Hours
      td4 = row.insertCell(3);
      td4.style.textAlign = "center";
      hiddenInHours =  otherWindow.document.createElement("input");
      hiddenInHours.type = 'hidden';
      hiddenInHours.value = numHours;
      hiddenInHours.name = "wdHours" + iteration;
      hiddenInHours.id = "wdHours" + iteration;
     td4.innerHTML = numHours;
      td4.appendChild(hiddenInHours);

    //iteration check for php side
    td5 = row.insertCell(4);
    td5.style.width = 0;
    hiddenPK = otherWindow.document.createElement("input");
    hiddenPK.type = "hidden";
    hiddenPK.name = "wdPK" + iteration;
    hiddenPK.id = "wdPK" + iteration;
    hiddenPK.value = "NEW";
    td5.appendChild(hiddenPK);
    repaintStripesToTable(); */
    window.close();
    }//end else
}

function isValidDate()
{
    if(!(isValidDay(month.value, day.value, year.value)
        && isValidHour(hour.value) && isValidMinute(minute.value)
        && isValidMonth(month.value) && isValidYear(year.value)
        ))
    {
       return false;
    }
    return true;
}

function isValidMonth(month)
{
    if(isNaN(parseInt(month)))
    {
        return false;
    }
    if (month < 1 || month > 12) return false;
    else return true;
}

function isValidDay(month, day, year)
{
    if(isNaN(parseInt(day)))
    {
        return false;
    }
    if(day < 1) return false;
    if((month == "2" || month == "02")
            && (year % 4) == 0)
    {
        //check for leap year
        if (day > 29) return false;
    }
    else
    {
        switch(month)
        {
            case 1:
                if(day > 31) return false;
                break;
            case 2:
                if(day > 28) return false;
                break;
            case 3:
                if(day > 31) return false;
                break;
            case 4:
                if(day > 30) return false;
                break;
            case 5:
                if(day > 31) return false;
                break;
            case 6:
                if(day > 30) return false;
                break;
            case 7:
                if(day > 31) return false;
                break;
            case 8:
                if(day > 31) return false;
                break;
            case 9:
                if(day > 30) return false;
                break;
            case 10:
                if(day > 31) return false;
                break;
            case 11:
                if(day > 30) return false;
                break;
            case 12:
                if(day > 31) return false;
                break;
        }
    }
    return true;
}

function isValidYear(year)
{
    if(isNaN(parseInt(year)))
    {
        return false;
    }
    if(year < 2000 || year > 3000)
    {
        return false;
    }
    return true;
}

function isValidHour(hour)
{
    if(isNaN(parseInt(hour)))
    {
        return false;
    }
    if(hour < 0 || hour > 23)
    {
        return false;
    }
    return true;
}

function isValidMinute(minute)
{
    if(minute.length < 2) return false;
    if(isNaN(parseInt(minute)))
    {
        return false;
    }
    if(minute > 59 || minute < 0)
    {
        return false;
    }
    return true;
}

function isValidAMPM(AMPM)
{
    var temp = toUpperCase(AMPM);
    alert(temp);
    if(temp == "AM" || temp == "PM")
    {
        return true;
    }
    else
    {
        return false;
    }
}
function repaintStripesToTable()
{
    table = window.opener.document.getElementById("tblWorkDone");
    for(i=0; i<table.rows.length; i++)
    {
        table.rows[i].style.backgroundColor = ((i % 2) == 0) ? "#666666" : "";
    }
}
</script>
</html>
